package com.loan.controller;
import java.util.Date;
import java.util.Map;

import com.jfinal.aop.Clear;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Page;
import com.loan.model.Data;
import com.loan.model.Notice;
import com.loan.model.NoticeLog;
import com.loan.model.User;
import com.loan.service.NoticeLogService;
import com.loan.service.NoticeService;
import com.loan.util.Json;
import com.loan.util.Str;

public class NoticeController extends Controller {
	private static NoticeService notice = new NoticeService();
	private static NoticeLogService noticeLog = new NoticeLogService();
	
	/**
	 *  获取未阅读的数量
	 */ 
	public void unReadMark() {
		User u = getAttr("user");
		String type = getPara("type","app");
		long activityNum = noticeLog.unReadNum(type+"_activity", u);
		long newsNum = noticeLog.unReadNum(type+"_news", u);
		long noticeNum = noticeLog.unReadNum(type+"_notice", u);
		long total = activityNum + newsNum + noticeNum;
		
		Notice n1 = notice.findByType(type+"_activity",u);
		Notice n2 = notice.findByType(type+"_news",u);
		Notice n3 = notice.findByType(type+"_notice",u);
		
		renderJson(Json.ok("数据获取成功！", Data.put("total",total, "activity_num", activityNum, "news_num",newsNum, "notice_num",noticeNum, "activity",n1,"news",n2,"notice",n3)));
	}	
	
	/**
	 *  获取列表公告
	 */
	public void list() {
		int cid = getAttrForInt("cid");
		User u = getAttr("user");
		int page = getParaToInt("page",1);
		int size = getParaToInt("size",10);
		Map<String,String> params = Data.flat(getParaMap());
		if(cid>1) {
			params.put("cid", String.valueOf(cid));
		}	 
		Page<Notice> p = notice.list(page, size, params);
		p.getList().forEach(n->{
			n.put("is_read", noticeLog.isRead(n.getId(), u.getId()) ? 1 : 0);
		});
		renderJson(Json.ok("数据获取成功！", Data.put("page", p)));
	}	
	
	/**
	 *  已阅读该内容
	 */
	public void setRead() {
		User u = getAttr("user"); 
		String ids = getPara("id");
		if(Str.nb(ids)) {
			for(String id : ids.split(",")) {
				// 判断是否已经读过
				if(!noticeLog.isRead(Integer.valueOf(id), u.getId())){
					NoticeLog nl = new NoticeLog();
					nl.setNoticeId(getParaToInt("id"));
					nl.setUserId(u.getId());
					nl.setCreateTime(new Date());
					nl.save();
				}
			} 
		}
		renderJson(Json.ok("数据保存成功！"));
	}

	
	/**
	 *  全部阅读该内容
	 */
	public void setAllRead() {
		User u = getAttr("user"); 
		String type = getPara("type","");
		for(Notice n : noticeLog.unRead(type, u)) {
			NoticeLog nl = new NoticeLog();
			nl.setNoticeId(n.getId());
			nl.setUserId(u.getId());
			nl.setCreateTime(new Date());
			nl.save();
		}
		renderJson(Json.ok("数据处理成功！"));
	}
	
	/**
	 *  模糊搜索
	 */
	public void search() {
		int page = getParaToInt("page",1);
		int size = getParaToInt("size",10);
		Page<Notice> p = notice.findByKey(page, size, getPara("key",""));
		renderJson(Json.ok("数据获取成功！", Data.put("page",p)));
	}
	
	/**
	 * 编辑消息
	 */
	public void edit() {
		Date d = new Date(); 
		int id = getParaToInt("id",0);
		Notice notice = id>0 ? Notice.dao.findById(id) : new Notice();
		notice.setName(getPara("name"));
		notice.setType(getPara("type"));
		notice.setStatus(getPara("status"));
		notice.setContents(getPara("contents"));
		 
		if(id>0?notice.update():notice.setCreateTime(d).save()) {
			renderJson(Json.ok("信息编辑成功！"));
		}
	}
	
	/**
	 *  获取公告信息
	 */
	@Clear
	public void info() {
		getResponse().setHeader("Access-Control-Allow-Origin","*"); 
		renderJson(Json.ok("数据获取成功！", Notice.dao.findById(getParaToInt("id",0))));
	}
	
	/**
	 * 获取系统用户协议
	 */ 
	@Clear
	public void statement() throws Exception {
		String type = getPara("type","riskWarn");
		int id = 0; 
		switch(type){
			case "riskWarn": id=1;break;	// 风险提示函
			case "aboutUs": id=2;break;	// 关于我们
			case "openAccount": id=3;break;	// 开户说明
			case "userAgreement": id=4;break;	// 用户协议
			case "depository": id=5;break;	// 银行存管
			case "security": id=6;break;	// 资金安全
			case "protect": id=7;break;	// 信息保护
			case "control": id=8;break;		// 风控体系
			case "commit": id=9;break;	// 投资人承诺书
		}
        renderJson(Json.ok("获取系统公告成功！", Data.put("item", Notice.dao.findById(id))));
	}

	/**
	 *  公告上线/下线
	 */
	public void status() {
		Notice n = Notice.dao.findById(getParaToInt("id",0));
		if(n!=null){
			n.setStatus(getPara("status")).update();
			renderJson(Json.ok("状态修改成功！"));
		}
	}
	
	/**
	 * 公告删除
	 */
	public void del() {
		Notice n = Notice.dao.findById(getParaToInt("id",0));
		if(n!=null){
			n.delete();
			renderJson(Json.ok("删除成功！"));
		}
	}	
}
 